/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.orderlang.pay.controller;

import java.sql.*;
import java.util.*;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.util.StringUtils;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.excel.ExportExcel;

import org.apache.lucene.index.Term;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.seeyoui.kensite.common.base.domain.Page;
import com.seeyoui.kensite.framework.luence.domain.LuceneDocument;
import com.seeyoui.kensite.framework.luence.util.LuceneUtils;
import com.seeyoui.kensite.framework.system.domain.SysUser;
import com.seeyoui.kensite.framework.system.util.UserUtils;
import com.seeyoui.kensite.bussiness.orderlang.pay.domain.AccountsReceivablePay;
import com.seeyoui.kensite.bussiness.orderlang.pay.domain.Pay;
import com.seeyoui.kensite.bussiness.orderlang.pay.domain.TransactionDetailsPay;
import com.seeyoui.kensite.bussiness.orderlang.pay.service.PayService;

/**
 * Pay
 * 
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Controller
@RequestMapping(value = "orderlang/pay")
public class PayController extends BaseController {

    @Autowired
    private PayService payService;

    /**
     * 展示列表页面
     * 
     * @param modelMap
     * @param module
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:view")
    @RequestMapping(value = "/{page}")
    public ModelAndView view(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
            @PathVariable String page) throws Exception {
        return new ModelAndView("bussiness/orderlang/pay/" + page, modelMap);
    }

    /**
     * 根据ID查询单条数据
     * 
     * @param modelMap
     * @param module
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:select")
    @RequestMapping(value = "/data/{id}")
    @ResponseBody
    public Object data(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, @PathVariable String id)
            throws Exception {
        Pay pay = payService.findOne(id);
        return pay;
    }

    /**
     * 根据ID查询单条数据并返回相应表单
     * 
     * @param modelMap
     * @param module
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:view")
    @RequestMapping(value = "/form/{page}/{id}")
    public ModelAndView form(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
            @PathVariable String page, @PathVariable String id) throws Exception {
        Pay pay = payService.findOne(id);
        modelMap.put("pay", pay);
        return new ModelAndView("bussiness/orderlang/pay/" + page, modelMap);
    }

    /**
     * 获取列表展示数据
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:select")
    @RequestMapping(value = "/list/data", method = RequestMethod.POST)
    @ResponseBody
    public Object listData(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay)
            throws Exception {
        List<Pay> payList = payService.findList(pay);
        int total = payService.findTotal(pay);
        EasyUIDataGrid eudg = new EasyUIDataGrid();
        eudg.setTotal(String.valueOf(total));
        eudg.setRows(payList);
        return eudg;
    }

    /**
     * 获取列表展示数据带分页器用于jsp自己做分页
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:view")
    @RequestMapping(value = "/list/page", method = RequestMethod.POST)
    @ResponseBody
    public Object listPage(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay)
            throws Exception {
        List<Pay> payList = payService.findList(pay);
        int total = payService.findTotal(pay);
        Page<Pay> page = new Page<Pay>(pay.getPage(), pay.getRows(), total);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("rows", payList);
        dataMap.put("page", page);
        return dataMap;
    }

    /**
     * 获取列表展示数据带分页器用于jsp自己做分页
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:view")
    @RequestMapping(value = "/findAccountsReceivableList", method = RequestMethod.POST)
    @ResponseBody
    public Object findAccountsReceivableList(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
            Pay pay, String orderCode, String orderTime, String status) throws Exception {
        Map map = new HashMap();
        map.put("row", pay.getRow());
        map.put("rows", pay.getRows());
        if(!StringUtils.isEmpty(orderTime) && orderTime.indexOf(" - ") > 0){
            String[] orderTimes = orderTime.split(" - ");
            map.put("orderBeginTime", orderTimes[0]);
            map.put("orderEndTime", orderTimes[1]);
        }
        map.put("orderCode", orderCode);
        map.put("status", status);
        List<AccountsReceivablePay> payList = payService.findListAccountsReceivable(map);
        AccountsReceivablePay accountsReceivablePay = payService.findListAccountsReceivableForSum(map);
        int total = payService.findTotalAccountsReceivable(map);
        Page<Pay> page = new Page<Pay>(pay.getPage(), pay.getRows(), total);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("rows", payList);
        dataMap.put("orderAmount", accountsReceivablePay.getOrderAmount());
        dataMap.put("payAmount", accountsReceivablePay.getPayAmount());
        dataMap.put("confirmAmount", accountsReceivablePay.getConfirmAmount());
        dataMap.put("page", page);
        return dataMap;
    }
    
    /**
     * 获取列表展示数据带分页器用于jsp自己做分页
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:view")
    @RequestMapping(value = "/findTransactionDetailsList", method = RequestMethod.POST)
    @ResponseBody
    public Object findTransactionDetailsList(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
            Pay pay) throws Exception {
        Map map = new HashMap();
        map.put("row", pay.getRow());
        map.put("rows", pay.getRows());
        List<TransactionDetailsPay> payList = payService.findListTransactionDetails(map);
        int total = payService.findTotalTransactionDetails(map);
        Page<Pay> page = new Page<Pay>(pay.getPage(), pay.getRows(), total);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("rows", payList);
        dataMap.put("page", page);
        return dataMap;
    }

    /**
     * 获取所有数据
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:select")
    @RequestMapping(value = "/list/all", method = RequestMethod.POST)
    @ResponseBody
    public Object listAll(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay) throws Exception {
        List<Pay> payList = payService.findAll(pay);
        return payList;
    }

    /**
     * 保存新增的数据
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:insert")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay) throws Exception {
        if (!beanValidator(modelMap, pay)) {
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        payService.save(pay);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 保存修改的数据
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay) throws Exception {
        if (!beanValidator(modelMap, pay)) {
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        payService.update(pay);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }
    
    /**
     * 确认收款的方法
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:update")
    @RequestMapping(value = "/confirmReceivables", method = RequestMethod.POST)
    @ResponseBody
    public String confirmReceivables(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            modelMap.put("message", "确认收款失败");
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        Pay pay = payService.findOne(id);
        if (null == pay) {
            modelMap.put("message", "确认收款失败");
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        // 获取当前登陆人
        SysUser sysUser = UserUtils.getUser();
        pay.setIsConfirm("1");
        pay.setConfirmorId(sysUser.getId());
        pay.setConfirmTime(new Date());
        payService.update(pay);
        modelMap.put("message", "确认收款成功");
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 删除数据库
     * 
     * @param modelMap
     * @param payId
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, String id)
            throws Exception {
        List<String> listId = Arrays.asList(id.split(","));
        payService.delete(listId);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 假删除
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:delete")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public String remove(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay) throws Exception {
        payService.remove(pay);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 导出Excel数据
     * 
     * @param modelMap
     * @param pay
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("orderlang:pay:export")
    @RequestMapping(value = "/export")
    public String export(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, Pay pay) throws Exception {
        String fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        List<Pay> payList = payService.findAll(pay);
        new ExportExcel(null, Pay.class).setDataList(payList).write(response, fileName).dispose();
        return null;
    }
}